package com.haust.dianqi.web;

import com.alibaba.fastjson.JSONObject;
import com.haust.dianqi.pojo.Article;
import com.haust.dianqi.pojo.UserFile;
import com.haust.dianqi.service.ArticleService;
import com.haust.dianqi.service.UserFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: csp1999
 * @Date: 2020/10/10/14:02
 * @Description: 文章controller
 */
@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private UserFileService userFileService;

    /*
     * 获取首页最新公告
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/13 22:09
     * @author: csp1999
     */
    @ResponseBody
    @GetMapping("/indexPublic")
    public JSONObject indexPublic(){
        JSONObject jsonObject = new JSONObject();

        List<Article> publics = articleService.queryCurrentPublic();
        System.out.println(publics);
        jsonObject.put("publics",publics);
        return jsonObject;
    }
    /*
     * 获取首页最新新闻
     * @return: com.alibaba.fastjson.JSONObject
     * @create: 2020/10/13 22:10
     * @author: csp1999
     */
    @ResponseBody
    @GetMapping("/indexNews")
    public JSONObject indexNews(){
        JSONObject jsonObject = new JSONObject();

        List<Article> news = articleService.queryCurrentNews();
        jsonObject.put("news",news);
        return jsonObject;
    }

    /*
     * 查看文章
     * @param: articleId
     * @param: model
     * @return: java.lang.String
     * @create: 2020/10/13 20:03
     * @author: csp1999
     */
    @GetMapping("/article/{articleId}")
    public String queryArticleById(@PathVariable("articleId") String articleId, Model model){
        Article article = articleService.queryArticleById(articleId);
        UserFile file = userFileService.findById(article.getFileId());
        model.addAttribute("article",article);
        model.addAttribute("file",file);

        //  根据articleId 查询中间表 select * from files_article where articleId = ‘’
        // List<files> fileList
        return "article";
    }

    /*
     * 文章列表
     * @param: type
     * @param: model
     * @return: java.lang.String
     * @create: 2020/10/13 20:03
     * @author: csp1999
     */
    @GetMapping("/articleList/{type}")
    public String articleList(@PathVariable("type") String type, Model model){
        String target ="";// 如果是新闻则return news; 如果是公告return pulic;

        if(type.equals("1")){// 新闻
            //字符串转换
            Integer t = Integer.parseInt(type);
            List<Article> list = articleService.getArticleByType(t);
            //System.out.println(list);
            model.addAttribute("articleList",list);
            target="news";
        }else if(type.equals("2")){// 公告
            Integer t = Integer.parseInt(type);
            List<Article> list = articleService.getArticleByType(t);
            model.addAttribute("articleList",list);
            target="public";
        }else if (type.equals("3")){// 规章制度(仅针对于本科教育)
            Integer t = Integer.parseInt(type);
            List<Article> list = articleService.getArticleByType(t);
            model.addAttribute("articleList",list);
            target="rules";
        }else {
            // 出错
            target = "error";
        }
        return target;
    }
}
